package com.test.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.test.cmn.client.DictFeignClient;
import com.test.common.exception.YyghException;
import com.test.common.result.ResultCodeEnum;
import com.test.hosp.repository.HospitalRepository;
import com.test.hosp.service.HospitalService;
import com.test.model.hosp.Hospital;
import com.test.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date 2021/8/2 -11:00
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    public HospitalRepository hospitalRepository;

    @Autowired
    public DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> map) {
        //是否存在,存在修改，不存在添加
        String mapString = JSONObject.toJSONString(map);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);

        if(hospitalExist != null){
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
        }else{
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
        }
        hospital.setUpdateTime(new Date());
        hospital.setIsDeleted(0);
        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hoscode == null){
            throw new YyghException(ResultCodeEnum.DATA_ERROR);
        }
        return hospital;
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable = PageRequest.of(page -1 ,limit);
        //设置条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospital,hospitalQueryVo);

        Example<Hospital> example = Example.of(hospital,matcher);

        Page<Hospital> all = hospitalRepository.findAll(example, pageable);
        //Hospital中没有医院等级，通过其map参数，进行封装医院等级
        all.getContent().parallelStream().forEach(item -> {
            this.setHospitalHostType(item);
        });

        return all;
    }

    /**
     * 对查询list集合，进行医院等级封装
     * @param item
     */
    private Hospital setHospitalHostType(Hospital item) {
        //医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", item.getHostype());
        //省 市 地区
        String provinceString = dictFeignClient.getName(item.getProvinceCode());
        String cityString = dictFeignClient.getName(item.getCityCode());
        String districtString = dictFeignClient.getName(item.getDistrictCode());

        item.getParam().put("hostypeString",hostypeString);
        item.getParam().put("fullAddress",provinceString + cityString + districtString);

        return item;
    }

    //更新医院上线状态
    @Override
    public void updateStatus(String id, Integer status) {
        //根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        //设置修改的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }


    @Override
    public Map<String, Object> getHospById(String id) {
        Map<String, Object> result = new HashMap<>();
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        //医院基本信息（包含医院等级）
        result.put("hospital",hospital);
        //单独处理更直观
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    //获取查询list集合，遍历进行医院等级封装
    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //查询省 市  地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString",hostypeString);
        return hospital;
    }


    //获取名称
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospital != null){
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return null;
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        return null;
    }
}
